package com.qingshu.controller.custom;

import com.qingshu.common.annotation.Log;
import com.qingshu.common.entity.custom.Music;
import com.qingshu.common.entity.custom.model.MusicAddModel;
import com.qingshu.common.entity.custom.model.MusicQueryModel;
import com.qingshu.common.entity.custom.model.MusicResourceModel;
import com.qingshu.common.entity.custom.model.MusicUpdateModel;
import com.qingshu.common.enums.BusinessType;
import com.qingshu.common.util.body.JsonResult;
import com.qingshu.common.util.body.ResultTool;
import com.qingshu.common.util.model.ModelUtil;
import com.qingshu.service.IMusicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huzz
 * @since 2021-12-09
 */
@Api(tags = "音乐管理")
@RestController
@RequestMapping("/custom/music")
public class MusicController {

    @Autowired
    private IMusicService musicService;


    @Log(title = "音乐业务模块-添加音乐", businessType = BusinessType.INSERT, isSaveResponseParam = true)
    @ApiOperation(value = "添加音乐")
    @PostMapping("/addMusic")
    public JsonResult addMusic(@RequestBody MusicAddModel model){
        return musicService.addMusic(model) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "音乐业务模块-批量添加音乐", businessType = BusinessType.INSERT, isSaveResponseParam = true)
    @ApiOperation(value = "批量添加音乐")
    @PostMapping("/addMusicByList")
    public JsonResult addMusicByList(@RequestBody List<MusicAddModel> modelList){
        return musicService.addMusicByList(modelList) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "操作日志", businessType = BusinessType.UPDATE, isSaveResponseParam = true)
    @ApiOperation(value = "根据id修改")
    @PutMapping("/updateMusicById")
    public JsonResult updateMusicById(@RequestBody MusicUpdateModel model){
        return musicService.updateMusicById((Music) ModelUtil.toEntityStatic(model, Music.class)) ? ResultTool.success() : ResultTool.fail();
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE, isSaveResponseParam = true)
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/delMusicById")
    public JsonResult delMusicById(@ApiParam(required = true) Integer musicId){
        return musicService.delMusicById(musicId) ? ResultTool.success() : ResultTool.fail("删除失败, 请检查是否存在数据关联");
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询分页列表")
    @GetMapping("/finMusicPage")
    public JsonResult findMusicPage(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                      @RequestParam(defaultValue = "10", required = false) Integer pageSize,
                                    MusicQueryModel model){
        return ResultTool.success(musicService.findMusicPage(pageNum, pageSize, model));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("/finMusicById")
    public JsonResult finMusicById(Integer musicId){
        return ResultTool.success(musicService.finMusicById(musicId));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据音乐名称进行查询")
    @GetMapping("/finUserByMusicName")
    public JsonResult finUserByRoleName(String musicName){
        return ResultTool.success(musicService.finUserByRoleName(musicName));
    }

    @Log(title = "操作日志", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询歌曲相关资源")
    @GetMapping("/getMusicResource")
    public JsonResult getMusicResource(MusicResourceModel model){
        return ResultTool.success(musicService.getMusicResource(model));
    }

    @Log(title = "操作日志-歌曲类型占比", businessType = BusinessType.SELECT)
    @ApiOperation(value = "歌曲类型占比")
    @GetMapping("/getMusicTypePercentage")
    public JsonResult getMusicTypePercentage(){
        return ResultTool.success(musicService.getMusicTypePercentage());
    }

}
